package at.ac.univie.zsu.aguataplan.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import at.ac.univie.zsu.aguataplan.dao.ClubDao;
import at.ac.univie.zsu.aguataplan.domain.Club;
import at.ac.univie.zsu.aguataplan.domain.ClubEMail;
import at.ac.univie.zsu.aguataplan.domain.ClubWeb;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;

/**
 * @author gerry
 * 
 */
@Service
public class ClubManagerBean implements ClubManager {
	private static Logger log = Logger.getLogger(ClubManagerBean.class);

	@Autowired
	private ClubDao clubDao;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public ClubManagerBean() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	@Override
	public void cleanClub() throws DatabaseException {
		try {
			clubDao.cleanClub();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entries", e.getMessage());
		}
	}

	@Override
	public Club addClub(String name) throws DatabaseException {
		Club club = new Club();
		club.setName(name);

		Club retClub;
		try {
			retClub = clubDao.addClub(club);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not insert entry", e.getMessage());
		}
		return retClub;
	}

	@Override
	public void deleteClub(long id) throws DatabaseException {
		try {
			clubDao.deleteClub(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entry", e.getMessage());
		}
	}

	@Override
	public List<Club> getClubList() throws DatabaseException {
		List<Club> list = null;

		try {
			list = clubDao.getClubList();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}
		return list;
	}

	@Override
	public Integer getClubListPosition(long id) throws DatabaseException {
		List<Club> list = null;
		Integer pos = -1;

		try {
			list = clubDao.getClubList();
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getId() == id) {
					pos = i;
					break;
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}

		return pos;
	}

	@Override
	public Club getClubByName(String name) {
		return clubDao.getClubByName(name);
	}

	@Override
	public Club mergeClub(Club club) throws DatabaseException {
		Club retClub = null;
		try {
			retClub = clubDao.mergeClub(club);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}
		return retClub;
	}

	@Override
	public ClubEMail mergeClubEMail(ClubEMail clubEMail) throws DatabaseException {
		Club club = clubEMail.getRefClub();

		Club c = null;

		try {
			c = clubDao.mergeClub(club);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}

		return clubEMail;
	}

	@Override
	public void deleteClubEMail(ClubEMail clubEMail) throws DatabaseException {
		Club club = clubEMail.getRefClub();
		club.deleteEMail(clubEMail.getId());

		try {
			clubDao.mergeClub(club);
			clubDao.deleteClubEMail(clubEMail.getId());
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entry", e.getMessage());
		}
	}

	@Override
	public List<ClubEMail> getClubEMailList(Club club) throws DatabaseException {
		List<ClubEMail> list = null;

		try {
			list = clubDao.getClubEMailList(club);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}

		return list;
	}

	@Override
	public List<ClubWeb> getClubWebList(Club club) throws DatabaseException {
		List<ClubWeb> list = null;

		try {
			list = clubDao.getClubWebList(club);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}
		return list;
	}

	@Override
	public void deleteClubWeb(ClubWeb clubWeb) throws DatabaseException {
		Club club = clubWeb.getRefClub();
		club.deleteWeb(clubWeb.getId());

		try {
			clubDao.mergeClub(club);
			clubDao.deleteClubWeb(clubWeb.getId());
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entry", e.getMessage());
		}
	}

	@Override
	public ClubWeb mergeClubWeb(ClubWeb clubWeb) throws DatabaseException {
		Club club = clubWeb.getRefClub();

		Club c = null;

		try {
			c = clubDao.mergeClub(club);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}

		return clubWeb;
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public ClubDao getClubDao() {
		return clubDao;
	}

	public void setClubDao(ClubDao clubDao) {
		this.clubDao = clubDao;
	}

}
